/**
 * 
 * @brief  net_protocol.h
 * @param  None
 * 
 * @author xhq
 * @date   2024/11/05
 * 
 */
#ifndef __SRC_NET_PROTOCOL_H__
#define __SRC_NET_PROTOCOL_H__

/* include */
#include "global_variables.h"
#include "typedef.h"
#include "bsp_net.h"
#include "bsp.h"
#include "bsp_check.h"
#include "rpmsg_modules.h"

/* define */
#define     NET_FRAME_HEAH              0xC0
#define     NET_FRAME_END               0xC0
#define     NET_SEND_FRAM_HEAD_LEN      28
#define     NET_LINK_ADDR               0x0000
#define     DEV_SN_NUM                  0x0002
#define     PROTOCOL_VERSION            0x10
#define     DETECT_MAX_TARGET_NUM       512

/* enum */
// 身份标识类型
typedef enum __IDENTIFY_TYPE
{
    SIGNAL_DEV = 1,     // 信号机
    MMW_LIDAR  = 7,     // 毫米波雷达交通状态检测器
    MEC        = 9,     // 边缘计算服务器
    TRAFC_PLAT = 10,    // 交通平台
    ID_TYPE_OTHER,      // 其他

}IDENTIFY_TYPE;

// 指令操作类型
typedef enum __OPERATION_TYPE
{
    QUERY_REQUEST                    = 0x80,    // 查询请求	
    SET_REQUEST                      = 0x81,    // 设置请求	
    PROACT_UPLOAD                    = 0x82,    // 主动上报	
    QUERY_RESPONSE                   = 0x83,    // 查询应答	
    SET_RESPONSE                     = 0x84,    // 设置应答	
    PROACT_UPLOAD_RESPONSE           = 0x85,    // 主动上传应答	
    ERROR_RESPONSE                   = 0x86,    // 出错应答	
    QUERY_START_REQUEST              = 0xA0,    // 查询开始请求	
    CIRCULAR_RESPONSE_INFO           = 0xA1,    // 循环应答信息	
    CIRCULAR_RESPONSE_CONFIRM_INFO   = 0xA2,    // 循环应答确认信息	
    OPERATION_TYPE_OTHER,                       // 其他

}OPERATION_TYPE;

// 指令操作对象
typedef enum __OPERATION_TARGET
{
    COMM_CONN                    = 0x0101,      // 通信连接
    DETECTOR_CONFIG_PARA         = 0x0204,      // 检测器配置参数
    DETECTOR_WORK_STATUS         = 0x0205,      // 检测器工作状态
    TRAFFIC_TARGET_TRAJ_INFO     = 0x0301,      // 交通目标轨迹信息
    DETECT_CROSS_PASS_INFO       = 0x0302,      // 检测断面过车信息
    TRAFFIC_STATUS_INFO          = 0x0303,      // 交通状态信息
    TRAFFIC_FLOW_INFO            = 0x0304,      // 交通流信息
    ABNORMAL_EVENT_INFO          = 0x0305,      // 异常事件信息
    SYSTEM_PARA                  = 0x8001,      // 系统参数
    TRAJ_TRACK_ALG_PARA          = 0x8002,      // 轨迹跟踪算法参数
    ORIGINAL_MIPI_DATA           = 0x8003,      // 原始mipi数据
    DSP_OUTPUT_DATA              = 0x8004,      // dsp输出数据
    OPERATION_TARGET_OTHER       = 0x8005,      // 其他

}OPERATION_TARGET;


/* typedef */
#pragma pack(1)

typedef struct __IDENTITY
{
    uint8_t     AreaCode[3];
    uint16_t    type;
    uint16_t    num;

}IDENTITY;

typedef struct __ORIGNAL_MIPI_REQ
{
    uint32_t    req_mipi_num;
    uint32_t    each_frame_data_len;
    uint8_t     rf_channel;     // 0x00--远端；0x02--近端
    uint16_t    each_chirp_data_len;
    uint16_t    chirp_num;
    uint8_t     data_type;      //0：ADC数据采集；    1：通道标定；      2：移相器标定，
    uint8_t     tx_num;         //1/2/4/8/16/32/64/128   仅上传数据类别为2:移相器标定时有效
    uint8_t     wave_braud;     //0.01GHz为单位 仅上传数据类别为1:通道标定时有效
    uint8_t     resvered[13];
    
} ORIGNAL_MIPI_REQ;

typedef struct __ORIGNAL_MIPI_RES
{
    uint32_t    req_total_frame_num;
    uint32_t    res_frame_cnt;
    uint8_t     recv_status;

}ORIGNAL_MIPI_RES;

typedef struct __UPLOAD_MIPI_DATA
{
    uint32_t    total_frame_num;
    uint32_t    frame_count;
    uint16_t    total_package_num;
    uint16_t    package_count;
    uint8_t     mipi_data[65536];

}UPLOAD_MIPI_DATA;

typedef struct __NET_FRAME_HEAD_TAB
{
    uint16_t    link_addr;
    IDENTITY    id_info_1;
    IDENTITY    id_info_2;
    uint8_t     protocol_ver;
    uint8_t     oper_type;
    uint16_t    oper_target;

}NET_FRAME_HEAD_TAB;

typedef struct __SINGLE_TARGET_INFO
{
    uint16_t    id;
    uint8_t     type;
    uint8_t     length;
    uint8_t     width;
    uint8_t     height;
    uint16_t    horiz_distance;
    uint16_t    vertic_distance;
    double      longitude;          // 经度
    double      latitude;           // 纬度
    float       altitude;
    uint8_t     lane_number;
    float       heading_angle;
    float       speed;
    float       acceleration;
    uint32_t    RCS;
    uint8_t     confidence;

}SINGLE_TARGET_INFO;

typedef struct __TRACK_INFO
{
    uint8_t     UTC_time[8];    // 前4个字节为UTC时间秒值，后4个字节是微秒
    uint16_t    target_num;
    SINGLE_TARGET_INFO   target[DETECT_MAX_TARGET_NUM];
    
}TRACK_INFO;

typedef struct __DSP_CAL_DATA_REQ
{
    uint32_t    req_dsp_frame_num;
    uint8_t     frame_type;         // 0x00--far anat;0x02--near anat;
    uint8_t     data_type;          // 0x00--fft calculate data
    uint8_t     enable_flag;        // 0x00--stop(upload track info);0x01--begin(upload dsp data)
    uint8_t     resvered[16];
    
} DSP_CAL_DATA_REQ;


typedef struct __DSP_CAL_TARGET_INFO
{
    uint32_t index_info;            // 目标索引（高16位为速度维度（0-127）， 低16位为距离维度（0-511））
    float    noise_SNR;             // 目标噪声信噪比
    float    velocity;              // 目标真实速度
    float    distance;              // 目标的距离
    float    azimuth;               // 目标的方位角
    float    DOA_snr;               // 测角信噪比
    float    Poi_peak_intense;      // 点峰值强度
    uint8_t  sped_resol_times;      // 速度解模糊倍数
    float    Phase_Array_Vari;      // 相位数组的方差

}DSP_CAL_TARGET_INFO;

typedef struct __UPLOAD_DSP_DATA
{
    uint32_t            total_frame_num;
    uint32_t            frame_count;
    uint8_t             UTC_time[8];    // 前4个字节为UTC时间秒值，后4个字节是微秒
    uint16_t            target_num;
    DSP_CAL_TARGET_INFO dsp_single_target_info[RADAR_MAX_OBJECT_NUM];

}UPLOAD_DSP_DATA;

#pragma pack()

/* global varibles */
// 原始mipi数据请求信息
extern ORIGNAL_MIPI_REQ g_mipi_req_info;

// dsp原始目标数据请求信息
extern DSP_CAL_DATA_REQ g_dsp_req_info;

// 发送方标识信息
extern IDENTITY g_net_recv_send_id;


/* function */
uint32_t net_anlysis_procotol(struct sockaddr * l_sockaddr, uint8_t *l_u8_recv_buf, uint32_t l_u32_data_len);
uint32_t wlr_add_protocol_head_tail(uint8_t *l_u8_send_data, uint32_t l_u32_data_len, uint8_t l_u8_master_cmd, uint8_t l_u8_subid_cmd);
void get_utc_time(uint8_t *p_buf);

static uint32_t wlr_01_02_raw_debug_adc_data(struct sockaddr * l_sockaddr, uint8_t *l_u8_recv_buf, uint8_t l_u8_master_cmd, uint8_t l_u8_subid_cmd);
static uint32_t wlr_03_04_broadcast_device_info(struct sockaddr * l_sockaddr, uint8_t *l_u8_recv_buf, uint8_t l_u8_master_cmd, uint8_t l_u8_subid_cmd);

static uint32_t wlr_04_01_device_protocol_version(struct sockaddr * l_sockaddr, uint8_t *l_u8_recv_buf, uint8_t l_u8_master_cmd, uint8_t l_u8_subid_cmd);
static uint32_t wlr_09_01_get_dsp_debug_param(struct sockaddr * l_sockaddr, uint8_t *l_u8_recv_buf, uint8_t l_u8_master_cmd, uint8_t l_u8_subid_cmd);
static uint32_t wlr_09_02_set_dsp_debug_param(struct sockaddr * l_sockaddr, uint8_t *l_u8_recv_buf, uint8_t l_u8_master_cmd, uint8_t l_u8_subid_cmd);

#endif